package xyz.molzhao.cfapi;

import xyz.molzhao.util.CommonUtil;

import java.util.concurrent.CompletableFuture;

/**
 * 场景：
 * 小明进门点菜，这时饭店饭还没蒸好，所以厨师开始做菜的同时，服务员需要开始蒸饭，小明打王者等待等到厨师和服务员
 * 蒸完饭做完菜，并且服务员打完饭，小明开始吃饭。
 * <p>
 * 等两个任务都执行完之后得到两个结果，再把两个结果加工成一个结果
 */
public class Combine {

    public static void other() {
        CommonUtil.createBehavior("小明进门");
        CommonUtil.createBehavior("小明点了番茄炒蛋");


        CompletableFuture<String> dish = CompletableFuture.supplyAsync(() -> {
            CommonUtil.createBehavior("厨师做菜", 1000);
            return "番茄炒蛋";
        });

        CompletableFuture<String> rice = CompletableFuture.supplyAsync(() -> {
            CommonUtil.createBehavior("服务员蒸饭", 2000);
            return "饭";
        });

        CommonUtil.createBehavior("小明打王者");
        String order = String.format("%s + %s", dish.join(), rice.join());
        CommonUtil.createBehavior("服务员打饭", 1000);
        CommonUtil.createBehavior(String.format("小明吃%s", order));
    }

    public static void main(String[] args) {
        CommonUtil.createBehavior("小明进门");
        CommonUtil.createBehavior("小明点了番茄炒蛋");

        CompletableFuture<String> order = CompletableFuture.supplyAsync(() -> {
            CommonUtil.createBehavior("厨师做菜", 1000);
            return "番茄炒蛋";
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            CommonUtil.createBehavior("服务员蒸饭", 2000);
            return "饭";
        }), (dish, rice) -> {
            CommonUtil.createBehavior("服务员打饭", 10);
            return dish + " + " + rice;
        });

        CommonUtil.createBehavior("小明打王者");
        CommonUtil.createBehavior(String.format("小明吃%s", order.join()));
    }
}
